package org.bluedream.core.module.liandi.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.collections.map.HashedMap;
import org.bluedream.comm.base.BaseServiceImpl;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.comm.utils.RestResult;
import org.bluedream.comm.utils.excel.ExcelUtil;
import org.bluedream.core.module.liandi.dao.MemberDao;
import org.bluedream.core.module.liandi.entity.Member;
import org.bluedream.core.utils.sms.Sms;
import org.bluedream.core.utils.sms.SmsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MemberService
 * @Description TODO
 * @Author Administrator
 * @Data 2021/12/16 8:59
 * @Version
 */
@Service
@DS("slave_1")
@Transactional(
        readOnly = true
)
public class MemberService extends BaseServiceImpl<MemberDao, Member> implements IService<Member> {

    public Member getById(String phone) {
        if (EmptyUtil.isEmpty(phone)){
            return new Member();
        }
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone" , phone);
        return super.getOne(queryWrapper);
    }

    public IPage<Member> findPage(Member entity, String... args) {
        Map<String , Object> search = new HashedMap();
        if (EmptyUtil.isNoEmpty(entity.getPhone())
                || EmptyUtil.isNoEmpty(entity.getName())
                || EmptyUtil.isNoEmpty(entity.getNickName())
        ){
            if (EmptyUtil.isNoEmpty(entity.getPhone())){
                search.put("phone" , entity.getPhone());
            }
            if (EmptyUtil.isNoEmpty(entity.getName())){
                search.put("name" , entity.getName());
            }
            if (EmptyUtil.isNoEmpty(entity.getNickName())){
                search.put("nick_name" , entity.getNickName());
            }
        }
        return super.findPage(entity , search , args);
    }

    public Sms initSms(String[] phones){
        return new Sms(phones);
    }

    public RestResult sendBatchSms(MultipartFile file){
        try {
            List<String> phones = ExcelUtil.import2List(file , 0 , 0);
            List<String> phoneList = new ArrayList<>();
            for (int i = 0; i < phones.size(); i++) {
                System.out.println(phones.get(i));
                phoneList.add(phones.get(i));
                if ((i + 1) % 50 == 0 || (i + 1) == phones.size()){
                    Sms sms = new Sms();
                    sms.setPhoneList(phoneList);
                    sms.setTemplateCode("SMS_231454200");
                    String resultMsg = SmsUtil.sendBatchSms(sms);
                    System.out.println(resultMsg);
                    phoneList.clear();
                }
            }
            return RestResult.ok();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }



    public RestResult sendBatchSms(Sms entity) {
        System.out.println(entity);
        try {
            List<String> phoneList = new ArrayList<>();
            List<String> nameList = new ArrayList<>();
            for (String s1:entity.getPhones()
                 ) {
                phoneList.add(s1);
                Member m1 = getById(s1);
                if (EmptyUtil.isNoEmpty(m1.getName())){
                    nameList.add(m1.getName());
                }
            }
//            todo:测试数据
/*            phoneList.add("15689120325");
            phoneList.add("18660256615");
            nameList.add("丁旭东");
            nameList.add("丁旭东");*/
            entity.setNameList(nameList);
            entity.setPhoneList(phoneList);


            //1.发送短信
//        sendSms( phoneList, msg);
            String resultMsg = SmsUtil.sendBatchSms(entity);
            return RestResult.ok(resultMsg);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return null;
    }
}